
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include "../imagepipe.h"


#define P2(x) ((x)*(x))


extern "C"
{


void applyFilter( const Image &img, Image &dst, const Rect &r )
{
	
	for( int x = r.x; x < r.w; x ++ ) {
		for( int y = r.y; y < r.h; y ++ ) {
			Rgb p = img.pixel( x, y );
			
			int r = RED(p), g = GREEN(p), b = BLUE(p);
			if( P2(r-g) + P2(r-b) + P2(g-b) > 3*P2(30) ) {
				// pure enough!
#ifndef MEMCOPY
				dst.pixel( x, y ) = p;
#endif
			}
			else {
				int avg = ( r + g + b ) / 3;
				dst.pixel( x, y ) = RGB( avg, avg, avg );
			}
		}
	}
	
}


}
